package com.zcc.securitydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        /*使用内存的方式来对用户进行角色分类，并赋予相应的权限*/
        /*这里创建一个admin/123456的 Admin角色，可以通过角色对相应的接口进行过滤*/
        auth.inMemoryAuthentication().withUser("admin").password("123456").roles("Admin");
    }




    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                /*对请求路径为/auth打头的进行拦截过滤*/
                .antMatchers("/auth/**")
                .fullyAuthenticated()
                .and()
                .httpBasic()
        ;
    }

    /**
     * 加解码器使用 不做转义的 ，可以理解为客户端明文
     */
    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
}
